package com.chenfan.controller;

import com.chenfan.pojo.Result;
import com.chenfan.pojo.User;
import com.chenfan.service.BookService;
import com.chenfan.service.UserService;
import com.chenfan.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
@RestController
public class LoginController {

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User loginUser=userService.login(user);
        if (loginUser != null) {
            final HashMap<String, Object> claims = new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");

    }


}
